<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="hdLeft">
          <img src="../assets/img/tx.jpg" alt />
          <span>电商后台管理系统</span>
        </div>
        <el-button @click="backLogin">退出登入</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollspse?'64px':'200px'">
          <div class="toggle" @click="toggleMenu">|||</div>
          <el-menu
            background-color="#323744"
            text-color="#fff"
            :unique-opened="true"
            active-text-color="#369BFF"
            :collapse="isCollspse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <el-submenu :index="''+item.path" v-for="item in MenuList" :key="item.id">
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item
                :index="'/'+item1.path"
                @click="setNavState('/'+item1.path)"
                :key="item1.id"
                v-for="item1 in item.children"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item1.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: "Home",
  components: {},
  data() {
    return {
      MenuList: [],
      icons: {
        "125": "iconfont icon-yonghuguanli",
        "103": "iconfont icon-quanxian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-dingdan",
        "145": "iconfont icon-shujutongji"
      },
      isCollspse: false,
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      this.MenuList = res.data;
    },
    backLogin() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    toggleMenu() {
      this.isCollspse = !this.isCollspse;
    },
    setNavState(path) {
      window.sessionStorage.setItem("activePath", path);
      this.activePath = path;
    }
  }
};
</script>
<style lang="less" scoped>
.el-container {
  height: 100vh;
}
.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .hdLeft {
    display: flex;
    align-items: center;
    img {
      border-radius: 50%;
      height: 60px;
    }
    span {
      margin-left: 10px;
      font-size: 22px;
      color: #fff;
    }
  }
}
.el-menu {
  border: none;
}
.el-main {
  background-color: #eaedf2;
}
.el-aside {
  background-color: #323744;
  .toggle {
    background-color: #282c34;
    height: 30px;
    line-height: 30px;
    color: #fff;
    letter-spacing: 0.3rem;
    text-align: center;
    cursor: pointer;
  }
}
.iconfont {
  margin-right: 10px;
}
</style>